﻿//------------------------------------------------------------------------------
// The contents of this file are subject to the nopCommerce Public License Version 1.0 ("License"); you may not use this file except in compliance with the License.
// You may obtain a copy of the License at  http://www.nopCommerce.com/License.aspx. 
// 
// Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. 
// See the License for the specific language governing rights and limitations under the License.
// 
// The Original Code is nopCommerce.
// The Initial Developer of the Original Code is NopSolutions.
// All Rights Reserved.
// 
// Contributor(s): _______. 
//------------------------------------------------------------------------------

using System;
using System.Web.Routing;
using NopSolutions.NopCommerce.BusinessLogic;
using NopSolutions.NopCommerce.BusinessLogic.Model;
using NopSolutions.NopCommerce.BusinessLogic.SEO;
using NopSolutions.NopCommerce.Common.Utils;


namespace NopSolutions.NopCommerce.Web.Modules
{
    public partial class TopicPageControl : BaseNopFrontendUserControl
    {
        private string _enteredPassword;
        private LocalizedTopic _localizedTopic = null;
        private Topic _topic = null;

        protected override void OnInit(EventArgs e) {
            //if (Request.RequestContext.RouteData.Route != null) {
            //    Route r = Request.RequestContext.RouteData.Route as Route;
            //    if (r != null && r.Url.StartsWith("pagini/")) {
            //        int topicId = 0;
            //        if (!int.TryParse(Request.RequestContext.RouteData.Values["topicId"].ToString(), out topicId)) {
            //            if (int.TryParse(Request.RequestContext.RouteData.Values["topicId"].ToString().Split('-')[0], out topicId)) {
            //                var topic = this.TopicService.GetTopicById(topicId);
            //                var topicSeName = SEOHelper.GetSEName(topic.Name);
            //                Response.RedirectToRoutePermanent("TopicRoute", new { topicId = topicId, topicName = topicSeName });
            //                Response.End();
            //            }
            //        }
            //    }
            //}

            base.OnInit(e);

            Load += new EventHandler(TopicPageControl_Load);
            PreRender += new EventHandler(TopicPageControl_PreRender);
            btnPassword.Click+=new EventHandler(btnPassword_Click);
        }

        void TopicPageControl_Load(object sender, EventArgs e) {
            this.BindData();

            if (this.LocalizedTopic != null) {
                //title
                string title = this.LocalizedTopic.MetaTitle;
                if (String.IsNullOrEmpty(title))
                    title = this.LocalizedTopic.Title;
                if (!string.IsNullOrEmpty(title))
                    SEOHelper.RenderTitle(this.Page, title, true);

                //meta
                if (!String.IsNullOrEmpty(this.LocalizedTopic.MetaDescription))
                    SEOHelper.RenderMetaTag(this.Page, "description", this.LocalizedTopic.MetaDescription, true);
                if (!String.IsNullOrEmpty(this.LocalizedTopic.MetaKeywords))
                    SEOHelper.RenderMetaTag(this.Page, "keywords", this.LocalizedTopic.MetaKeywords, true);
            }
        }

        void TopicPageControl_PreRender(object sender, EventArgs e) {
            //default button
            this.txtPassword.Attributes.Add("onkeydown", "if(event.which || event.keyCode){if ((event.which == 13) || (event.keyCode == 13)) {document.getElementById('" + btnPassword.ClientID + "').click();return false;}} else {return true}; ");
        }

        private void BindData() {
            if (this.LocalizedTopic != null) {
                bool passwordOK = ValidatePassword();
                if (passwordOK) {
                    phPassword.Visible = false;
                    lTitle.Visible = true;
                    lBody.Visible = true;

                    if (!string.IsNullOrEmpty(this.LocalizedTopic.Title)) {
                        lTitle.Text = Server.HtmlEncode(this.LocalizedTopic.Title);
                    }
                    else {
                        lTitle.Visible = false;
                    }
                    if (!string.IsNullOrEmpty(this.LocalizedTopic.Body)) {
                        lBody.Text = this.LocalizedTopic.Body;
                    }
                    else {
                        lBody.Visible = false;
                    }
                }
                else {
                    phPassword.Visible = true;
                    lTitle.Visible = false;
                    lBody.Visible = false;
                }
            }
            else {
                Response.Redirect(CommonHelper.GetStoreLocation());
            }
        }

        private bool ValidatePassword() {
            bool passwordOK = true;
            if (this.Topic != null) {
                if (this.Topic.IsPasswordProtected &&
                    !this.Topic.Password.Equals(this.EnteredPassword)) {
                    passwordOK = false;
                }
            }
            return passwordOK;
        }

        protected void btnPassword_Click(object sender, EventArgs e) {
            this.EnteredPassword = txtPassword.Text;
            bool passwordOK = ValidatePassword();
            if (!passwordOK) {
                pnlMessage.Visible = true;
                lError.Text = GetLocaleResourceString("TopicPage.WrongPassword");
            }
            BindData();
        }

        public LocalizedTopic LocalizedTopic {
            get {
                if (_localizedTopic == null) {
                    _localizedTopic = this.TopicService.GetLocalizedTopic(this.TopicId, NopContext.Current.WorkingLanguage.LanguageId);
                }
                return _localizedTopic;
            }
        }

        public Topic Topic {
            get {
                if (_topic == null) {
                    _topic = this.TopicService.GetTopicById(this.TopicId);
                }
                return _topic;
            }
        }

        public int TopicId {
            get {
                return CommonHelper.RouteStringInt("topicId");
            }
        }

        public string EnteredPassword {
            get { return _enteredPassword; }
            set { _enteredPassword = value; }
            //get {
            //    object obj2 = this.ViewState["EnteredPassword"];
            //    if (obj2 != null)
            //        return (string)obj2;
            //    else
            //        return string.Empty;
            //}
            //set {
            //    this.ViewState["EnteredPassword"] = value;
            //}
        }
    }
}
